// 商机搜索表单指令
define('mainSearchTableDirective', ['angular'], function (angular) {
    var m = angular.module('directives');
    m.directive('mainSearchTable', [function () {
        return {
            restrict: 'AE',
            templateUrl: 'js/templates/mainSearchTable.html',
            replace: true,
            scope:{
                listData:"="
            },
            link: function ($scope, elem, attr) {
                // $scope.elem=$(elem).find("input[type='checkbox']");
                // $scope.selectAll=false;
            },
            controller:['$scope','$rootScope','$route','$window','$filter','userServers','businessServers',function($scope,$rootScope,$route,$window,$filter,userServers,businessServers){
                $scope.checked=[];
                $scope.selectedData=[];
                $scope.allSelect=function(){
                    $scope.checked.forEach(function(v,i){
                        $scope.checked[i]=!v;
                    });
                };
                $scope.$watch('checked',function(n,o){
                    $scope.selectedData=[];
                    var tempObj={};
                    $scope.checked.forEach(function(v,i){
                        tempObj={};
                        if(v){
                            tempObj.id=$scope.listData[i].id;
                            tempObj.stick=$scope.listData[i].stick;
                            $scope.selectedData.push(tempObj);
                        }
                    });
                },true);
                $scope.stickSubmit=function(){
                    $scope.selectedData.forEach(function(v,i){
                        $scope.selectedData[i].stick=v.stick===0?1:0;
                    });
                    businessServers.setMultitermBusiness($scope.selectedData).then(function(data){
                        // console.log(data);
                        if(data){
                            $route.reload();
                        }else{
                            alert("修改失败");
                        }
                    });
                };
                $scope.data=angular.copy(angular.fromJson($rootScope.user.custom_list));
                if($scope.data===null){
                    $scope.data={"id":1,"qq":1,"sign_out":1,"order_id":1,"name":1,"consultant":1,"school":1,"channel_manager":1,"time":1,"overdue_num":1,"visit":1,"branch_company":1,"email":1,"positioning":1,"phone":1,"sign":1};
                }
                $scope.searchTableData=angular.copy($scope.data);//模态框的数据
                $scope.searchTableShow=angular.copy($scope.data);//搜索框的数据
//              console.log($rootScope.user.custom_search_list);
                $scope.setSearchTable=function(){
                    $scope.data={};
                    $scope.data.id=$rootScope.user.id;
                    $scope.data.custom_list=angular.toJson($scope.searchTableData);
                    userServers.setUser($scope.data).then(function(data){
                        if(data.data){
                            $('#searchTable').modal('toggle');
                            $rootScope.user.custom_list=angular.copy($scope.searchTableData);
                            $window.localStorage.crm_user=JSON.stringify($rootScope.user);
                            $route.reload();
                        }else{
                            alert("修改失败！");
                        }
                    });
                };
                $scope.currentData={};
                $scope.modalOpen=function(id){
                    businessServers.getBusinessById(id).then(function(data){
                        $scope.currentData=data[0];
                        $scope.currentData.follow_up_records=angular.fromJson($scope.currentData.follow_up_records);
                    });
                };
                //添加跟进记录
                $scope.followRecords={};
                $scope.addFollowRecords=function(data){
                    if(confirm('是否提交?')===false)
                        return false;
                    if(data){
                        if ($scope.currentData.follow_up_records===null) {
                            $scope.currentData.follow_up_records=[];
                        }
                        $scope.currentData.follow_up_records.push($scope.followRecords);
                        $scope.currentData.follow_up_records=$filter('orderBy')($scope.currentData.follow_up_records,'time',true);
                        $scope.currentData.follow_up_records=angular.toJson($scope.currentData.follow_up_records);
                        $scope.currentData = $filter('simplifyJsonFilter')($scope.currentData);
                        $scope.submitData={id:$scope.currentData.id,follow_up_records:$scope.currentData.follow_up_records};
                        $scope.arr=[$scope.submitData];
                        businessServers.setMultitermBusiness($scope.arr).then(function(data){
                            if(data){
                                $('#followRecords').modal('hide');
                                $route.reload();
                            }else{
                                alert("添加失败");
                            }
                        });
                    }else{
                        alert("请填写全部信息");
                    }

                };
            }]
        };
    }]);
});